using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.Windows.Forms;

namespace Evaluant.EussDSL.CustomCode
{
    /// <summary>
    /// Simple class used to display a dropdown list for boolean values
    /// </summary>
    public class BooleanNames : UITypeEditor
    {
        private IWindowsFormsEditorService formsEditorService;

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context != null)
            {
                return UITypeEditorEditStyle.DropDown;
            }
            return base.GetEditStyle(context);
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if ((context == null) ||
                (provider == null) || (context.Instance == null))
            {
                return base.EditValue(context, provider, value);
            }

            ListBox names = new ListBox();
            names.Sorted = true;
            names.Click += new EventHandler(listBox_Click);
            names.BorderStyle = BorderStyle.None;
            names.Items.Add("true");
            names.Items.Add("false");

            // Handle the service 
            this.formsEditorService =
                (IWindowsFormsEditorService)provider.
                GetService(typeof(IWindowsFormsEditorService));
            this.formsEditorService.DropDownControl(names);

            // Result 
            return names.SelectedItem;

            // Default behavior 
            //return base.EditValue(context, provider, value);
        }

        private void listBox_Click(object sender, EventArgs e)
        {
            if (this.formsEditorService != null)
            {
                this.formsEditorService.CloseDropDown();
            }
        }
    }
}
